package com.fishingwithme.android.ui.screens.map

import com.tencent.tencentmap.mapsdk.maps.model.Marker
import com.tencent.tencentmap.mapsdk.maps.model.Polygon
import com.tencent.tencentmap.mapsdk.maps.model.Polyline

/**
 * Context class to hold map operation data and elements that can be shared across handlers
 * This allows for better control and cleanup of map resources across different handlers
 */
class MapOperationContext {
    // Shared data map
    val data: MutableMap<String, Any> = mutableMapOf()

    // Map elements that can be managed
    private val markers: MutableList<Marker> = mutableListOf()
    private val polygons: MutableList<Polygon> = mutableListOf()
    private val polylines: MutableList<Polyline> = mutableListOf()

    /**
     * Add a marker to the context for management
     */
    fun addMarker(marker: Marker) {
        synchronized(markers) {
            markers.add(marker)
        }

    }

    /**
     * Add a polygon to the context for management
     */
    fun addPolygon(polygon: Polygon) {
        synchronized(polygons) {
            polygons.add(polygon)
        }

    }

    /**
     * Add a polyline to the context for management
     */
    fun addPolyline(polyline: Polyline) {
        synchronized(polylines) {
            polylines.add(polyline)
        }

    }

    /**
     * Remove a specific marker from management and the map
     */
    fun removeMarker(marker: Marker) {
        marker.remove()
        synchronized(markers) {
            markers.remove(marker)
        }

    }

    /**
     * Remove a specific polygon from management and the map
     */
    fun removePolygon(polygon: Polygon) {
        polygon.remove()
        synchronized(polygons) {
            polygons.remove(polygon)
        }

    }

    /**
     * Remove a specific polyline from management and the map
     */
    fun removePolyline(polyline: Polyline) {
        polyline.remove()
        synchronized(polylines) {
            polylines.remove(polyline)
        }

    }

    /**
     * Clear all markers from the map and management
     */
    fun clearMarkers() {
        synchronized(markers) {
            markers.forEach { it.remove() }
            markers.clear()
        }

    }

    /**
     * Clear all polygons from the map and management
     */
    fun clearPolygons() {
        synchronized(polygons) {
            polygons.forEach { it.remove() }
            polygons.clear()
        }

    }

    /**
     * Clear all polylines from the map and management
     */
    fun clearPolylines() {
        synchronized(polylines) {
            polylines.forEach {
                it.remove()
            }
            polylines.clear()
        }

    }

    /**
     * Clear all map elements from the map and management
     */
    fun clearAllElements() {
        clearMarkers()
        clearPolygons()
        clearPolylines()
    }

    /**
     * Get all managed markers
     */
    fun getMarkers(): List<Marker> = markers.toList()

    /**
     * Get all managed polygons
     */
    fun getPolygons(): List<Polygon> = polygons.toList()

    /**
     * Get all managed polylines
     */
    fun getPolylines(): List<Polyline> = polylines.toList()
}